var projetDreamIntervention = {
    infos: {},
    initDemandeIntervention: function () {
        projetDreamIntervention.infos = {
            messageOK: null
        };
        $(document).on("click", ".demanderIntervention", function () {
            var adresses = projetDreamConnexion.infos.personne.adressesDTO;

            var getRevesUrl = projetDream.webservicesBaseUrl + "catalogue/getAllReves";
            var req = $.ajax({
                url: getRevesUrl,
                dataType: "json"
            });
            req.done(function (result) {
                var reves = result;
                require(["jsviews"], function () {
                    var targetString = "#myModal";
                    var template = $.templates("#demandeInterventionTemplate");
                    projetDreamIntervention.infos.adresses = adresses;
                    projetDreamIntervention.infos.reves = reves;
                    template.link(targetString, {infos: projetDreamIntervention.infos});
                    projetDreamIntervention.initCreateIntervention();
                });
            });
        });
    },
    initCreateIntervention: function () {
        var form = $("#demandeInterventionForm");
//        var inputs = $("#demandeInterventionForm input, #demandeInterventionForm select");
//        inputs.attr("disabled", "disabled");
        form.on("submit", function (e) {
            e.preventDefault();
            var demandeInterventionUrl = projetDream.webservicesBaseUrl + "webservice/addIntervention";
            var data = form.serializeArray();
            var idAdresse = $("#demandeInterventionForm [name=idAdresse]").val();
            var adresse = projetDreamConnexion.getAdresseById(idAdresse);
            var adresseString = adresse.noRue + " " + adresse.rue + ", " + adresse.cp
                + " " + adresse.ville + ", " + adresse.pays;
            data.push({
                name: "adresseClient",
                value: adresseString
            });
            var client = projetDreamConnexion.getClient();
            data.push({
                name: "nomClient",
                value: client.nom
            });
            data.push({
                name: "prenomClient",
                value: client.prenom
            });
            var req = $.ajax({
                url: demandeInterventionUrl,
                data: data,
                type: "POST",
                dataType: "text"
            });
            req.done(function (result) {
                require(["jsobservable"], function(){
                    $.observable(projetDreamIntervention.infos).setProperty("messageOK", result);
                });
            });
        });
    }
};
